package com.shelpe.services.algorithm.client;

import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.algorithm.client.RankClient.RankClientFallback;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@FeignClient(name="${feign.simba.name}", url="${feign.simba.url}", fallback=RankClientFallback.class)
public interface RankClient {
	public final static Long[] TEMPLATE = new Long[0];
	
	@RequestMapping(path = "/simba/taobao.simba.tools.items.top.get", method = RequestMethod.GET)
	KeywordRankItemResponse findKeywordRanks(@RequestParam("nick")String nick, 
			@RequestParam("keyword")String keyword, @RequestParam("ip")String ips);
	
	@Data
	public static class KeywordRankItemResponse extends SimbaProxyBaseDto{
		@JsonProperty("simba_tools_items_top_get_response")
		private KeywordRankItemRes res;
	}
	
	@Data
	public static class KeywordRankItemRes{
		@JsonProperty("rankeditems")
		private KeywordRankItems dataList;
	}
	
	@Data
	public static class KeywordRankItems{
		@JsonProperty("ranked_item")
		private List<KeywordRankItem> items;
	}
	@Data
	public static class KeywordRankItem{
		private String nick;
		private int order;
		@JsonProperty("max_price")
		private String maxPrice;
		private String title;
		@JsonProperty("link_url")
		private String linkUrl;
		@JsonProperty("rank_score")
		private int rankScore;
	}
	
	@RequestMapping(path = "/simba/taobao.simba.keywords.realtime.ranking.batch.get", method = RequestMethod.GET)
	KeywordRealtimeRankItemResponse findRealtimeRanks(@RequestParam("nick")String nick, @RequestParam("ad_group_id")long adgroupID, 
			@RequestParam("bidword_ids")Long[] keywordIDs);
	
	@Data
	public static class KeywordRealtimeRankItemResponse extends SimbaProxyBaseDto{
		@JsonProperty("simba_keywords_realtime_ranking_batch_get_response")
		private KeywordRealtimeRankItemsResult res;
	}
	@Data
	public static class KeywordRealtimeRankItemsResult{
		@JsonProperty("result")
		private KeywordRealtimeRankItemsRes result;
	}
	@Data
	public static class KeywordRealtimeRankItemsRes{
		@JsonProperty("realtime_rank_list")
		private KeywordRealtimeRankItems dataList;
	}
	@Data
	public static class KeywordRealtimeRankItems{
		@JsonProperty("result")
		private List<KeywordRealtimeRankItem> items;
	}
	@Data
	public static class KeywordRealtimeRankItem{
		@JsonProperty("bidwordid")
		private long keywordID;
		@JsonProperty("state")
		private int state;
		@JsonProperty("pc_rank")
		private int pcRank;//PC的排名:(0:首页左侧位置;1:首页右侧第1;2:首页右侧第2,3:首页右侧第3,4:首页非前三,5:第2页,6:第3页,7:第4页,8:第5页,9:5页以后)
		@JsonProperty("mobile_rank")
		private int moibleRank;//移动的排名(0:移动首条,1:移动前三,3:移动4~6条,6:移动7~10条,10:移动11~15条,11:移动16~20条,12:20条以后)
	}

	@Service
	@Slf4j
	public static class RankClientFallback implements RankClient{

		@Override
		public KeywordRankItemResponse findKeywordRanks(String nick, String keyword, String ips) {
			log.error("fallback findKeywordRanks: {}, {}", nick, keyword);
			SimbaProxyBaseDto.Error error = new SimbaProxyBaseDto.Error(500, "fallback");
			
			KeywordRankItemResponse res = new KeywordRankItemResponse();
			res.setError(error);
			return res;
		}

		@Override
		public KeywordRealtimeRankItemResponse findRealtimeRanks(String nick, long adgroupID, Long[] keywordIDs) {
			log.error("fallback findRealtimeRanks: {}, {}", nick, adgroupID, keywordIDs);
			SimbaProxyBaseDto.Error error = new SimbaProxyBaseDto.Error(500, "fallback");
			
			KeywordRealtimeRankItemResponse res = new KeywordRealtimeRankItemResponse();
			res.setError(error);
			return res;
		}

	
	}
}
